home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 January / enter-2004-01.iso / files / freeciv-1.14.0-win32-sound.exe / {app} / doc / README.AI.txt < prev    next >
Encoding:
Text File  |  2003-01-21  |  15.3 KB  |  371 lines

  1. ==============
  2. THE FREECIV AI
  3. ==============
  4.  
  5.  
  6. CONTENTS
  7. ========
  8. Introduction
  9. Contacting the current AI developers
  10. Long-term AI development goals
  11. Want calculations
  12. Amortize
  13. Estimation of profit from a military operation
  14. Selecting military units
  15. Diplomacy
  16. Difficulty levels
  17. Things that needs to be fixed
  18. Idea space
  19.  
  20.  
  21. INTRODUCTION 
  22. ============ 
  23.  
  24. The Freeciv AI is widely recognized as being as good as or better
  25. militarywise as the AI of certain other games it is natural to compare
  26. it with.  It does, however, lack diplomacy and can only do war.  It is
  27. also too hard on novice players and too easy for experienced players.
  28.  
  29. The code base is not in a good state.  It has a number of problems,
  30. from very messy and not very readable codebase, to many missing
  31. features to some bugs, which aren't easy to fix due to unreadable
  32. code.  The problem is, that most the code was written by someone who
  33. didn't care about code readibility a lot.  After he left the project,
  34. various people have contributed their own mostly unfinished hacks
  35. without really fixing the main issues in the AI code, resulting in
  36. even more mess.
  37.  
  38. Another problem is that not all code is residing in ai/ (which is
  39. currently still linked to a server, but there're plans to separate
  40. this completely to some kind of client, working name is "civbot"), but
  41. it is also dissolved in little chunks in the whole server/.  Aside
  42. that, server/settlers.c is only AI stuff - the problem is, that most
  43. of it is used also for the autosettlers, so we can't separate it from
  44. the server.
  45.  
  46. This file aims to describe all such problems, in addition to various
  47. not entirely self-describing constants and equations used in the code
  48. commonly.
  49.  
  50.  
  51. CONTACTING THE CURRENT AI DEVELOPERS
  52. ====================================
  53.  
  54. AI development has its own mailing list. Send questions to
  55. freeciv-ai@freeciv.org, or go to 
  56.  
  57.   http://www.freeciv.org/mailinglists.html
  58.  
  59. to read the archives or to join up.
  60.  
  61.  
  62. LONG-TERM AI DEVELOPMENT GOALS
  63. ==============================
  64.  
  65. The long-term goals for Freeciv AI development are
  66.  -> to create a challenging and fun AI for human players to defeat
  67.  -> to create modular AI code that can easily be assembled into new AI
  68.     clients
  69.  -> to have multiple different AI clients compete against each other
  70.  
  71. In order to get to this point, the current AI code will be moved from the
  72. server and to the client. This requires that the AI code is separated
  73. completely from the server, and that clients get the (optional) 
  74. possibility of an omniscience cheat.
  75.  
  76. An important step is to move the goto code into the client. Also, the
  77. current CMA agent will split out its core calculations for use in
  78. client-side AI that do not use agents.
  79.  
  80. The final directory structure should look like this:
  81.  
  82.  client/agents      - this is agent territory
  83.  client/ai          - this is where AI implementations go
  84.  client/ai/common   - this is where common AI code should go
  85.  client/ai/XYZ      - AI implementation named XYZ
  86.  
  87.  server/            - no AI code allowed
  88.  ai/                - removed
  89.  
  90. While code is being moved and integrated, we will link the AI in the
  91. server with client/ai/common/libaicommon.a and client/ai/XYZ/libxyz.a, 
  92. making a gradual progress of files and features possible.
  93.  
  94.  
  95. WANT CALCULATIONS
  96. =================
  97.  
  98. Build calculations are expressed through a structure called ai_choice. 
  99. This has a variable called "want", which determines how much the AI 
  100. wants whatever item is pointed to by choice->type. choice->want is
  101.  
  102.    -199   get_a_boat
  103.    < 0    an error
  104.    == 0   no want, nothing to do
  105.    <= 100 normal want
  106.     > 100 critical want, used to requisition emergency needs
  107.     > ??? probably an error (1024 is a reasonable upper bound)
  108.     > 200 Frequently used as a cap. When want exceeds this value,
  109.           it is reduced to a lower number.
  110.  
  111. These are ideal numbers, your mileage while travelling through the 
  112. code may vary considerably.  Technology and diplomats, in particular, 
  113. seem to violate these standards.
  114.  
  115.  
  116. AMORTIZE
  117. ========
  118.  
  119. Hard fact:
  120. amortize(benefit, delay) returns benefit * ((MORT - 1)/MORT)^delay
  121. (where "^" == "to the power of")
  122.  
  123. Speculation:
  124. What is better, to receive 10$ annually starting in 5 years from now
  125. or 5$ annually starting from this year?  How can you take inflation
  126. into account?  The function amortize is meant to help you answer these
  127. questions.  To achieve this, it rescales the future benefit in terms of
  128. todays money.
  129.  
  130. Suppose we have a constant rate of inflation, x percent.  Then in five
  131. years time 10$ will buy as much as 10*(100/(100+x))^5 will buy today.
  132. Denoting 100/(100+x) by q we get the general formula, N dollars Y
  133. years from now will be worth N*q^Y in todays money.  If we will
  134. receive N every year starting Y years from now, the total amount
  135. receivable (in todays money) is N*q^Y / (1-q) --- this is the sum of
  136. infinite geometric series.  This is exactly the operation that
  137. amortize performs, the multiplication by some q < 1 raised to power Y.
  138. Note that the factor 1/(1-q) does not depend on the parameters N and Y
  139. and can be ignored.  The connection between MORT constant and the
  140. inflation rate x is given by
  141.     (MORT - 1) / MORT = q = 100 / (100 + x).
  142. Thus the current value of MORT = 24 corresponds to the inflation rate
  143. (or the rate of expansion of your civ) of 4.3%
  144.  
  145. Most likely this explanation is not what the authors of amortize() had
  146. in mind, but the basic idea is correct: the value of the payoff decays
  147. exponentially with the delay.
  148.  
  149. The version of amortize used in the military code (military_amortize())
  150. remains a complete mystery.
  151.  
  152.  
  153. ESTIMATION OF PROFIT FROM A MILITARY OPERATION
  154. ==============================================
  155.  
  156. This estimation is implemented by kill_desire function (which isn't
  157. perfect: multi-victim part is flawed) plus some corrections.  In
  158. general,
  159.         Want = Operation_Profit * Amortization_Factor
  160. where 
  161.  
  162. * Amortization_Factor is completely beyond me (but it's a function of the
  163. estimated time length of the operation).
  164.  
  165. * Operation_Profit = Battle_Profit - Maintenance
  166.  
  167. where
  168.  
  169. * Maintenance 
  170.   = (Support + Unhappiness_Compensation) * Operation_Time 
  171.   (here unhappiness is from military unit being away from home
  172.    and Support is the number of shields spent on supporting this unit 
  173.    per turn )
  174.  
  175. * Battle_Profit
  176.   = Shields_Lost_By_Enemy * Probability_To_Win 
  177.     - Shields_Lost_By_Us * Probability_To_Lose
  178.  
  179. That is Battle_Profit is a probabilistic average.  It answer the
  180. question "how much better off, on average, we would be from attacking
  181. this enemy unit?"
  182.  
  183.  
  184. SELECTING MILITARY UNITS
  185. ========================
  186.  
  187. The code dealing with choosing military units to be built and targets
  188. for them is especially messy.  Here is what we've managed to decipher.
  189.  
  190. Military units are requested in military_advisor_choose_build
  191. function.  It first considers the defensive units and then ventures
  192. into selection of attackers (if home is safe).  There are 2
  193. possibilities here: we just build a new attacker or we already have an
  194. attacker which was forced, for some reason, to defend.  In the second
  195. case it's easy: we calculate how good the existing attacker is and if
  196. it's good, we build a defender to free it up.
  197.  
  198. Building a brand-new attacker is more complicated.  Firstly,
  199. ai_choose_attacker_* functions are charged to find the first
  200. approximation to the best attacker that can be built here.  This
  201. prototype attacker is selected using very simple attack_power * speed
  202. formula.  Then (already in kill_something_with) we search for targets
  203. for the prototype attacker (using find_something_to_kill).  Having
  204. found a target, we do the last refinement by calling
  205. process_attacker_want to look for the best attacker type to take out
  206. the target.  This type will be our attacker choice.  Note that the
  207. function process_attacker_want has side-effects wrt the tech selection. 
  208.  
  209. Here is an example:
  210.  
  211. First ai_choose_attacker_land selects a Dragoon because it's strong
  212. and fast.  Then find_something_to_kill finds a victim for the
  213. (virtual) Dragoon, an enemy Riflemen standing right next to the town.
  214. Then process_attacker_want figures out that since the enemy is right
  215. beside us, it can be taken out easier using an Artillery.  It also
  216. figures that a Howitzer would do this job even better, so bumps up our
  217. desire for Robotics.
  218.  
  219. This is the idea, anyway.  In practice, it is more complicated and
  220. probably less effecient.
  221.  
  222.  
  223. DIPLOMACY
  224. =========
  225.  
  226. At the moment, the AI cannot change its diplomatic state.  The AI
  227. starts out in NO_CONTACT mode, and proceeds to WAR on first-contact.
  228.  
  229. However, AI understands the notion of "ally" and if, by some trickery
  230. ("teams" patch or direct savegame hacking), it is put in an alliance
  231. with another player, it will stick to this allience.  Thus, AI knows
  232. about friendly units and cities, and does not consider them to be
  233. either targets nor dangers.  Caravans are sent to friendly cities, and
  234. ships that do not have targets are sent on a goto to the closest
  235. allied port for the hull to get mended and for the crew to rest and
  236. befriend local girls.
  237.  
  238. AI is currently totally trusting and does not expect diplomatic states
  239. to ever change.  So if one is to add active diplomacy to the AI, this
  240. must be changed.
  241.  
  242. For people who want to hack at this part of the AI code, please note
  243.  * pplayers_at_war(p1,p2) returns FALSE if p1==p2
  244.  * pplayers_non_attack(p1,p2) returns FALSE if p1==p2
  245.  * pplayers_allied(p1,p2) returns TRUE if p1==p2 
  246.  * pplayer_has_embassy(p1,p2) returns TRUE if p1==p2
  247. i.e. we do not ever consider a player to be at war with himself, we
  248. never consider a player to have any kind of non-attack treaty with
  249. himself, and we always consider a player to have an alliance with
  250. himself. 
  251.  
  252. The introduction of diplomacy is fraught with many problems.  One is
  253. that it usually gains only human players, not AI players, since humans
  254. are so much smarter and know how to exploit diplomacy, while for AIs
  255. they mostly only add constraints on what it can do.  Another is that it
  256. can be very difficult to write diplomacy that is useful for and not in
  257. the way of modpacks.  Which means diplomacy either has to be optional,
  258. or have finegrained controls on who can do what diplomatic deals to
  259. whom, set from rulesets.
  260.  
  261. But one possibility for diplomacy that it would be easy to introduce,
  262. is an initial PEACE mode for AIs under 'easy' difficulty.  This can be
  263. turned to WAR by a simple countdown timer started after first contact.
  264. This way 'easy' will be more easy --- a frequently requested feature.
  265.  
  266.  
  267. DIFFICULTY LEVELS
  268. =================
  269.  
  270. There are currently three difficulty levels: 'easy', 'medium' and
  271. 'hard'.  The 'hard' level is no-holds-barred, while 'medium' has a
  272. number of handicaps.  In 'easy', the AI also does random stupid things
  273. through the ai_fuzzy function. 
  274.  
  275. The handicaps used are:
  276.   H_RATES, can't set its rates beyond government limits
  277.   H_TARGETS, can't target anything it doesn't know exists
  278.   H_HUTS, doesn't know which unseen tiles have huts on them
  279.   H_FOG, can't see through fog of war
  280.  
  281. The other defined handicaps (in common/player.h) are not currently in 
  282. use.
  283.  
  284.  
  285. THINGS THAT NEED TO BE FIXED
  286. ============================
  287.  
  288. * The AI difficulty levels aren't fully implemented.  Either add more
  289. handicaps to 'easy', or use easy diplomacy mode.
  290. * AI doesn't understand when to become DEMOCRACY or FUNDAMENTALIST.
  291. Actually it doesn't evalute governments much at all.
  292. * Cities don't realize units are on their way to defend it.
  293. * AI doesn't understand that some wonders are obsolete, that some 
  294. wonders become obsolete, and doesn't upgrade units.
  295. * AI doesn't understand how to favor trade when it needs luxury.
  296. * AI builds cities without regard to danger at that location.
  297. * Food tiles should be less wanted if city can't expand.
  298. * AI won't build cross-country roads outside of city radii.
  299. [Note: There is patch that permits the AI to build cross country
  300. roads/rail.  Unfortunately, it makes it too easy for the AI to be
  301. invaded.]
  302. * Non-military units need to stop going where they will be killed.
  303. * Locally_zero_minimap is not implemented when wilderness tiles 
  304. change.
  305. * Settlers won't treat about-to-be-built ferryboats as ferryboats.
  306. * If no path to chosen victim is found, new victim should be chosen.
  307. * AI doesn't know how to make trade routes or when.  It should try to 
  308. build trade routes for its best cities (most building bonuses and 
  309. least corruption) by moving caravans there and changing homecity.
  310. * Boats sometimes sail away from landlocked would-be passengers.
  311. * Ferryboats crossing at sea might lead to unwanted behavior.
  312. * Emergencies in two cities at once aren't handled properly.
  313. * AI sometimes will get locked into a zero science rate and stay 
  314. there.
  315. * Explorers will not use ferryboats to get to new lands to explore.
  316. * AI autoattack is never activated (probably a good thing too) (PR#1340)
  317. * AI sometimes believes that wasting a horde of weak military units to
  318. kill one enemy is profitable (PR#1340)
  319. * Stop building ships and shore defense in landlocked cities with a
  320. pond adjacent.
  321. * Make the AI building evaluation code use the new buildings.ruleset.
  322. * Create a function that gives a statistically exact value for a units
  323. chance of winning a battle.  [Now done.  What about expected number
  324. of hit points remaing, or the variance?  Can we come up with clever
  325. ways for the ai to use this information?]
  326. * Make a function that can generate a warmap for airplanes.
  327. * Convert the ai_manage_diplomat() function to use the warmap.
  328. * Make the AI respect FoW. (They don't get much bigger than this...)
  329. * Move goto code to common code
  330. * Teach the AI to Fortify units in non-city strategic positions. Also, it
  331. needs to not idle all it's units every turn, breaking 2 turn fortify.
  332. * Teach the AI to leave units alone in a turn to regain hit points. (it
  333. seems to have no concept of this at all!)
  334. * Stop the AI from trying to buy capitalization...
  335. * Fix the AI valuation of supermarket. (It currently never builds it).
  336. See farmland_food() and ai_eval_buildings() in advdomestic.c
  337. * Teach the AI to coordinate the units in an attack (ok, this one is a bit
  338. big...)
  339. * Teach the AI to use ferryboats to transport explorers to unexplored land.
  340. See ai_manage_explorer() and ai_manage_ferryboat().
  341.  
  342.  
  343. THINGS PEOPLE ARE WORKING ON (for latest info ask on AI list)
  344. ===============================================================
  345.  
  346. * teach AI to use planes and missiles. [GB]
  347. * teach AI to use diplomats [Per]
  348. * teach AI to do diplomacy (see Diplomacy section) [Per]
  349.  
  350.  
  351. IDEA SPACE
  352. ==========
  353.  
  354. * Friendly cities can be used as beachheads
  355. * Assess_danger should acknowledge positive feedback between multiple 
  356. attackers
  357. * Urgency and grave_danger should probably be ints showing magnitude 
  358. of danger
  359. * It would be nice for bodyguard and charge to meet en-route more 
  360. elegantly.
  361. * It may be correct to starve workers instead of allowing disorder to 
  362. continue.  Ideal if CMA code or similar is used here.
  363. * Bodyguards could be used much more often.  Actually it would be nice
  364. if the bodyguard code was taken out and shot, too.  Then rewritten, of
  365. course.
  366. * struct choice should have a priority indicator in it.  This will
  367. reduce the number of "special" want values and remove the necessity to
  368. have want capped, thus reducing confusion.
  369. * City tile values could be cached.  However, caching was tried by
  370. Raimar and was deemed unsuccessful. 
  371.